package com.im.app.controller;

import com.im.app.authConfig.CurrentUser;
import com.im.app.authConfig.RequireLogin;
import com.im.app.model.Admin;
import com.im.app.model.User;
import com.im.app.model.Welcome;
import com.im.app.service.WelcomeService;
import com.im.app.utils.Response;
import com.im.app.utils.ResponseResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping(value = "/welcome")
public class WelcomeController extends BaseController{

    @Resource
    private WelcomeService welcomeService;

    @RequireLogin
    @GetMapping(value = "/getAll")
    public ResponseResult<List<Welcome>> getAll(@RequestHeader String token) {
        User user = getUser(token);
        List<Welcome> welcomeList = welcomeService.queryWelcomeList(user.getUserId());
        return Response.makeOKRsp(welcomeList);
    }



    @RequireLogin
    @GetMapping(value ="/find")
    public ResponseResult<Welcome> findWelcome(Integer id) {
        try {
            //调用业务层返回列表
            Welcome welcome = welcomeService.findWelcome(id);
            return Response.makeOKRsp(welcome);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.makeErrRsp("查询欢迎语信息异常");
        }
    }

    @RequireLogin
    @GetMapping(value ="/delete")
    public ResponseResult deleteWelcome(Integer id) {
        int result = welcomeService.deleteWelcome(id);
        if(result>0){
            return Response.makeOKRsp("删除成功");
        }else{
            return Response.makeErrRsp("删除失败");
        }
    }


    @RequireLogin
    @PostMapping(value ="/add")
    public ResponseResult<Object> addWelcome(@CurrentUser User user,@RequestBody Welcome welcome) {
        welcome.setUserId(user.getUserId());
        int result = welcomeService.addWelcome(welcome);
        if(result>0){
            return Response.makeOKRsp("添加成功");
        }else{
            return Response.makeErrRsp("添加失败");
        }
    }

    @RequireLogin
    @PostMapping(value ="/adds")
    public ResponseResult<Object> addWelcomes(@CurrentUser User user,@RequestBody List<Welcome> welcomes) {
        int result = 0;
        for (Welcome welcome:welcomes) {
            welcome.setUserId(user.getUserId());
            result += welcomeService.addWelcome(welcome);
        }
        if(result>0){
            return Response.makeOKRsp("添加成功");
        }else{
            return Response.makeErrRsp("添加失败");
        }
    }

    @RequireLogin
    @PostMapping(value ="/update")
    public ResponseResult<Object> updateWelcome(@RequestHeader String token,@RequestBody Welcome welcome) {
        User user = getUser(token);
        if(welcome.getId() == null){
            return Response.makeErrRsp("修改失败");
        }
        welcome.setUserId(user.getUserId());
        int result = welcomeService.updateWelcome(welcome);
        if(result>0){
            return Response.makeOKRsp("修改成功");
        }else{
            return Response.makeErrRsp("修改失败");
        }

    }

    @RequireLogin
    @PostMapping(value ="/editWelcome")
    public ResponseResult<Object> editWelcome(@CurrentUser User user,@RequestBody List<Welcome> list) {
        if(user == null || user.getUserId() == null){
            return Response.makeErrRsp("用户不存在");
        }
        if(list.size()>0){
            for (Welcome welcome : list) {
                welcome.setUserId(user.getUserId());
                welcomeService.updateWelcome(welcome);
            }
        }else{
            return Response.makeErrRsp("参数不能为空");
        }
        return Response.makeOKRsp("修改成功");
    }


    @RequireLogin
    @PostMapping(value ="/admin/add")
    public ResponseResult<Object> adminAddWelcome(@RequestHeader String token,@RequestBody List<Welcome> welcomes) {
        Admin admin = getAdmin(token);
        if (admin == null){
            return Response.makeRsp(401,"登录已过期");
        }
        int result = 0;
        for (Welcome welcome:welcomes) {
            welcome.setUserId(admin.getId() + "");
            result += welcomeService.addWelcome(welcome);
        }
        if(result>0){
            return Response.makeOKRsp("添加成功");
        }else{
            return Response.makeErrRsp("添加失败");
        }
    }

    @RequireLogin
    @GetMapping(value ="/admin/delete")
    public ResponseResult<Object> adminDeleteWelcome(Integer id) {
        int result = welcomeService.deleteWelcome(id);
        if(result>0){
            return Response.makeOKRsp("删除成功");
        }else{
            return Response.makeErrRsp("删除失败");
        }
    }

    @GetMapping(value = "/admin/getAll")
    public ResponseResult<List<Welcome>> getAdminAll(@RequestHeader String token) {
        Admin admin = getAdmin(token);
        if (admin == null){
            return Response.makeRsp(401,"登录已过期");
        }
        List<Welcome> welcomeList = welcomeService.queryWelcomeList(admin.getId()+"");
        return Response.makeOKRsp(welcomeList);
    }

    @RequireLogin
    @PostMapping(value ="/admin/update")
    public ResponseResult<Object> adminUpdateWelcome(@RequestHeader String token,@RequestBody Welcome welcome) {
        Admin admin = getAdmin(token);
        if (admin == null){
            return Response.makeRsp(401,"登录已过期");
        }
        welcome.setUserId(admin.getId()+"");
        int result = welcomeService.updateWelcome(welcome);
        if(result>0){
            return Response.makeOKRsp("修改成功");
        }else{
            return Response.makeErrRsp("修改失败");
        }

    }

    @RequireLogin
    @PostMapping(value ="/admin/editWelcome")
    public ResponseResult<Object> adminEditWelcome(@RequestHeader String token,@RequestBody List<Welcome> list) {
        Admin admin = getAdmin(token);
        if (admin == null){
            return Response.makeRsp(401,"登录已过期");
        }
        if(list.size()>0){
            for (Welcome welcome : list) {
                welcome.setUserId(admin.getId()+"");
                welcomeService.updateWelcome(welcome);
            }
        }else{
            return Response.makeErrRsp("参数不能为空");
        }
        return Response.makeOKRsp("修改成功");
    }

}
